package com.liu.common.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.liu.common.entity.qo.AgreeWithAddQO;
import com.liu.common.entity.qo.GetGroupUserQO;
import com.liu.common.entity.qo.SendGroupMsgQO;
import com.liu.common.entity.vo.QQImageOCRVO;
import com.liu.mybatisPlus.q.entity.base.QGroupUser;
import com.liu.qqRobot.event.enums.MessageSubTypeEnum;
import com.liu.qqRobot.event.enums.MessageTypeEnum;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class QQClientUtils {

    /**
     * 判断是否是群消息
     * @param messageType messageType
     * @param subType subType
     * @return Boolean
     */
    public static Boolean checkIsGroup(String messageType, String subType) {
        return messageType.equals(MessageTypeEnum.GROUP.getType()) && subType.equals(MessageSubTypeEnum.NORMAL.getType());
    }

    /**
     * 发送群消息
     * @param msg 消息
     * @param groupId 群号
     * @param isGroup 是否是群消息
     */
    public static void sendMsg(String msg, Long userId, Long groupId, Boolean isGroup) {
        SendGroupMsgQO sendGroupMsgQO = new SendGroupMsgQO();
        sendGroupMsgQO.setMessage(msg);
        sendGroupMsgQO.setGroupId(groupId);
        sendGroupMsgQO.setUserId(userId);
        String url;
        if (isGroup) {
            url = "http://localhost:3000/send_group_msg";
        } else {
            // 私聊消息加at的CQ码无法发送
            sendGroupMsgQO.setMessage(msg.replace("[CQ:at,qq=" + userId + "]\n", ""));
            url = "http://localhost:3000/send_private_msg";
        }
//        log.info("发送QQ群消息，请求体：{}", sendGroupMsgQO.toString());
        HttpRequest.post(url)
                .body(sendGroupMsgQO.toString())
                .timeout(60000)
                .execute();
    }

    /**
     * 获取群聊用户
     * @param groupId groupId
     * @return List<QGroupUser>
     */
    public static List<QGroupUser> getGroupUser(Long groupId) {
        GetGroupUserQO getGroupUserQO = new GetGroupUserQO();
        getGroupUserQO.setGroupId(groupId);
        getGroupUserQO.setAutoEscape(false);
        String body = HttpRequest.post("http://localhost:3000/get_group_member_list")
                .body(getGroupUserQO.toString())
                .timeout(60000)
                .execute()
                .body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        return jsonArray.toList(QGroupUser.class);
    }

    /**
     * 处理加好友请求
     * @param flag flag
     */
    public static void agreeWithAddFriend(String flag) {
        AgreeWithAddQO agreeWithAddQO = new AgreeWithAddQO();
        agreeWithAddQO.setFlag(flag);
        agreeWithAddQO.setApprove(true);
        HttpRequest.post("http://localhost:3000/set_friend_add_request")
                .body(agreeWithAddQO.toString())
                .timeout(60000)
                .execute()
                .body();
    }

    /**
     * 处理邀请加群请求
     * @param flag flag
     */
    public static void agreeWithAddGroup(String flag, String subType) {
        AgreeWithAddQO agreeWithAddQO = new AgreeWithAddQO();
        agreeWithAddQO.setFlag(flag);
        agreeWithAddQO.setApprove(true);
        agreeWithAddQO.setSubType(subType);
        HttpRequest.post("http://localhost:3000/set_group_add_request")
                .body(agreeWithAddQO.toString())
                .timeout(60000)
                .execute()
                .body();
    }

    /**
     * QQ文字识别
     * @param filePath
     * @return
     */
    public static QQImageOCRVO OCR(String filePath) {
        File file = new File(filePath);
        String encode = Base64.encode(file);
        Map<String, String> requestBody = new HashMap<>();
        requestBody.put("image", "base64://" + encode);
        String responseBody = HttpRequest.post("http://localhost:3000/ocr_image")
                .header("Content-Type", ContentType.JSON.getValue())
                .body(JSONUtil.toJsonStr(requestBody))
                .timeout(60000)
                .execute()
                .body();
        return JSONUtil.toBean(responseBody, QQImageOCRVO.class);
    }

    /**
     * 上传群文件
     * @param groupId groupId
     * @param name name
     * @param filePath filePath
     */
    public static void uploadGroupFile(Long groupId, String name, String filePath) {
        Map<String, String> requestBody = new HashMap<>();
        requestBody.put("group_id", String.valueOf(groupId));
        requestBody.put("name", name);
        requestBody.put("file", filePath);
        HttpRequest.post("http://localhost:3000/upload_group_file")
                .header("Content-Type", ContentType.JSON.getValue())
                .body(JSONUtil.toJsonStr(requestBody))
                .execute()
                .body();
    }
}
